<?php

namespace app\admin\controller;

use app\admin\model\Upload;
use app\admin\model\User as UserModel;
use app\admin\validate\InserUser;
use app\admin\validate\RegiseteredUser;
use app\BaseController;
use app\tool\Controller\CacheController;
use app\tool\Controller\ComputingController;
use app\tool\Controller\StrController;
use app\tool\Controller\TokenController;
use app\tool\Controller\UploadController;
use think\facade\Config;
use think\Request;

class UserController extends BaseController {
	/*
		             *生成token 写入缓存
	*/
	public function index() {
		$tokenData = array(
			'openid' => 1234567896,
			'uniacid' => 1,
		);
		$token = TokenController::createToken($tokenData);
		$openidToken = CacheController::set($tokenData['openid'], $token, Config::get('app.token_exp_time'));
		return $token['token'];
	}

	/*
		             *解密token
	*/
	public function send() {
		$openidToken = CacheController::get('1234567896');
		$checkToken = TokenController::checkToken($openidToken['token']);
		var_dump($checkToken);
	}

	public function uploadImg(Request $request) {
		$folder = $this->real($this->getData, 'folder', Config::get('app.qi_niu.folder'));
		$url = $this->real($this->getData, 'url', '');
		$title = $this->real($this->getData, 'title', '');
		$insertData = [];
		if ($request->file('fileOne')) {
			$insertData['fileOne'] = UploadController::upload($request->file('fileOne'), $folder, $url, 'fileOne');

		}
		if ($request->file('fileTwo')) {
			$insertData['fileTwo'] = UploadController::upload($request->file('fileTwo'), $folder, $url, 'fileTwo');

		}
		$insertData['title'] = $title;
		$insertData['create_time'] = date('Y-m-d H:i:s');

		if ($request->file('fileOne') && $request->file('fileTwo') && $title) {
			upload::insert($insertData);
			return json_encode([$insertData['fileOne'], $insertData['fileTwo']]);
			// return 'OK';
		} else {
			return $insertData['fileOne'];
		}

	}
	public function uploadOne(Request $request) {
		$folder = $this->real($this->getData, 'folder', Config::get('app.qi_niu.folder'));
		$url = $this->real($this->getData, 'url', '');
		$title = $this->real($this->getData, 'title', '');
		$insertData = [];
		if ($request->file('file')) {
			$img = UploadController::upload($request->file('file'), $folder, $url, 'file');
			echo $img;
			exit;
		}

	}

	/*字符串加密*/
	public function strEncode($string) {
		return StrController::encode($string, Config::get('app.str_key'));
	}

	/*字符串解密*/
	public function strDecode($string) {
		return StrController::decode($string, Config::get('app.str_key'));
	}

	public function computing() {
		$one = 7;
		$two = 8;
		echo ComputingController::commonUse('+', $one, $two, 2);
		echo '=============';
		echo ComputingController::commonUse('-', $one, $two, 2);
		echo '=============';
		echo ComputingController::commonUse('*', $one, $two, 2);
		echo '=============';
		echo ComputingController::commonUse('/', $one, $two, 2);
	}

	public function insert(Request $request) {
		$validate = new InserUser();
		$result = $validate->check($request->param());
		if (!$result) {
			echo $validate->getError();
		} else {
			echo 11;
		}
	}

	public function userDetail() {
		return $this->success('用户信息', $this->user);
	}

	public function registered(RegiseteredUser $validate) {
		$result = $validate->check($this->getData); //字段校验
		if (!$result) {
			return $this->error($validate->getError());
		} else {
			$this->getData['pass_word'] = StrController::encode($this->getData['pass_word'], Config::get('app.str_key')); //密码加密
			$regisetered = UserModel::regisetered($this->getData); //写库
			if ($regisetered[0]) {
				return $this->success($regisetered[1]);
			} else {
				return $this->error($regisetered[1]);
			}
		}
	}
}
